<template>
  <div class="login-container">
    <div class="card">
      <h2>登录</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input type="text" id="username" v-model="username" required>
        </div>
        <div class="form-group">
          <label for="password">密码:</label>
          <input type="password" id="password" v-model="password" required>
        </div>
        <button type="submit">登录</button>
      </form>
      <p class="switch-link">没有账号？<router-link to="/register">立即注册</router-link></p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user'; // 确保路径正确

// 设置默认账号密码
const username = ref('23002969');
const password = ref('123456');
const router = useRouter();

const handleLogin = async () => {
  if (!username.value || !password.value) {
    alert('用户名和密码不能为空！');
    return;
  }

  try {
    const response = await fetch('/api/demo/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        username: username.value,
        pwd: password.value,
      }),
    });

    const res = await response.json();
    // 兼容后端返回格式 { errno: 0, data: { id, username } }
    if (res.errno === 0 && res.data && res.data.id) {
      alert('登录成功！');
      console.log('登录成功:', res.data);
      const userStore = useUserStore();
      userStore.setUserId(res.data.id);
      userStore.setUserToken(res.data.id.toString());
      userStore.setUsername(res.data.username || '');
      router.push('/');
    } else {
      alert(`登录失败: ${(res.message || (res.data && res.data.message)) || '密码错误或账号不存在'}`);
      console.error('登录失败:', res);
    }
  } catch (error) {
    alert('请求失败，请检查网络或服务器。');
    console.error('请求失败:', error);
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5; /* 柔和的背景色 */
}

.card {
  background-color: #ffffff;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08); /* 更柔和的阴影 */
  max-width: 400px;
  width: 100%;
  text-align: center;
}

h2 {
  color: #333333;
  margin-bottom: 30px;
  font-size: 28px;
  font-weight: 600;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

label {
  display: block;
  margin-bottom: 8px;
  color: #555555;
  font-weight: 500;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  box-sizing: border-box;
  font-size: 16px;
  transition: border-color 0.3s ease;
}

input[type="text"]:focus,
input[type="password"]:focus {
  border-color: #6a82fb; /* 聚焦时的边框颜色 */
  outline: none;
}

button {
  width: 100%;
  padding: 12px 20px;
  background-image: linear-gradient(to right, #6a82fb, #fc5c7d); /* 渐变按钮 */
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 18px;
  font-weight: 600;
  margin-top: 20px;
  transition: opacity 0.3s ease;
}

button:hover {
  opacity: 0.9;
}

.switch-link {
  margin-top: 25px;
  font-size: 15px;
  color: #777777;
}

.switch-link router-link {
  color: #6a82fb;
  text-decoration: none;
  font-weight: 500;
}

.switch-link router-link:hover {
  text-decoration: underline;
}
</style>